package edu.bbu.bibliospring.api.util;

import edu.bbu.bibliospring.api.exception.BibliospringApiException;
import org.apache.log4j.Logger;

import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

/**
 * Created by erika on 5/26/14.
 */

public class ApiExceptionMapper implements ExceptionMapper<Exception> {

    private static final Logger LOG = Logger.getLogger (ApiExceptionMapper.class);

	@Override
	public Response toResponse(final Exception e) {

		if (e instanceof BibliospringApiException) {
			return Response.status (Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
		}

        if (e instanceof NotAcceptableException) {
            return Response.status (Response.Status.BAD_REQUEST).entity (e.getMessage ()).build ();
        }

        if (e instanceof  NotFoundException) {
            return Response.status (Response.Status.BAD_REQUEST).entity (e.getMessage ()).build ();
        }
        LOG.error ("Converting Error to Api Response", e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity ("Internal server error").build ();
	}

}
